<?php

namespace App\Http\Controllers;

use App\Article;
use App\Banner;
use App\Cat;
use App\Data;
use App\Map;
use App\Http\Resources\ArticleCollection;
use App\Http\Resources\BannerCollection;
use App\Http\Resources\CatCollection;
use App\Http\Resources\DataCollection;
use App\Http\Resources\MapCollection;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;


class IndexController extends Controller
{
    //首页数据
    public function index()
    {
        //首页分类
        $cats = Cat::whereBetween('type', [1, 7])->get();
        $cats = new CatCollection($cats);
        $data['cats'] = collect($cats)->groupBy(['type', 'father_id']);
        $data['banner'] = new BannerCollection(Banner::where('is_show', 1)->where('type',0)->limit(5)->orderBy('sort', 'desc')->get());
        //首页Banner
        $where['is_del'] = 0;
        $where['status'] = 1;
        $datas = Data::where($where)->orderBy('created_at', 'desc')->get()->groupBy('type');
        foreach ($datas as $k => $re) {
            $data['items'][$k] = new DataCollection($re->take(15));
        }
        $data['items'][6] = new MapCollection(Map::limit(4)->orderBy('created_at', 'desc')->get());
        $data['items'][7] = new ArticleCollection(Article::limit(12)->where($where)->orderBy('created_at', 'desc')->get());
        $data['status'] = 1;
        $data['message'] = 'success';
        return response()->json($data);
    }

    //每周福利Api+底部广告
    public function weekWelfare()
    {
        $data['weekWelfare'] = new \App\Http\Resources\Banner(Banner::where('type', 2)->orderBy('id','desc')->first());
        $data['bottomad'] = Banner::where('type', 3)->whereDate('published_at','>',Carbon::now()->toDateString())->orderBy('id','desc')->first();
        if($data['bottomad']){
            $data['bottomad'] = new \App\Http\Resources\Banner($data['bottomad']);
        }
        return $data;
    }

    public function vipdata()
    {
        $where['is_del'] = 0;
        $where['status'] = 1;
        $datas = Data::where($where)->where('is_vip',1)->orderBy('created_at', 'desc')->get()->groupBy('type');
        foreach ($datas as $k => $re) {
            $data['items'][$k] = new DataCollection($re->take(6));
        }
//        全景
        $panoramic = Data::where($where)->where('cat_id2',410)->limit(6)->orderBy('created_at', 'desc')->get();
        $data['items'][8] = new DataCollection($panoramic);
        $data['items'][7] = new ArticleCollection(Article::limit(6)->where($where)->orderBy('created_at', 'desc')->get());
        $data['status'] = 1;
        $data['message'] = 'success';
        return response()->json($data);
    }

//      下载示例文件
    public function downdemo()
    {
        $file = public_path('/admin/demo.rar');
        $filename = '上传模型示例文件.rar';
        return response()->download($file, $filename);
    }
}
